package ink.ykb.j2CacheDemo.config;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import ink.ykb.j2CacheDemo.util.ServiceLock;

@Configuration
@Aspect
@Order(10)
public class ServiceAopConf {

		private Logger logger = LoggerFactory.getLogger(ServiceAopConf.class);  
		
		private static  Lock lock = new ReentrantLock(true);//互斥锁 参数默认false，不公平锁 
		
		@Around("@annotation(serviceLock)")
		public Object serviceLockAop(ProceedingJoinPoint pjp,ServiceLock serviceLock) throws Throwable{
		
			try {
				lock.lock();
				return pjp.proceed();//调用原方法
			} catch (Exception e) {
				throw e;
			}finally {
				lock.unlock();
			}
		}
		

}
